package cn.com.headfree.evaluation.dao.impl;

import cn.com.headfree.entity.TDeclareEvaluationInfo;
import cn.com.headfree.entity.TDeclareEvaluationPoint;
import cn.com.headfree.evaluation.dao.IEvaluationPointManageDao;
import cn.com.headfree.vo.TDeclareEvaluationPointVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2022/6/28
 **/
@Repository("evaluationPointManageDao")
public class EvaluationPointManageDaoImpl extends BaseDaoImpl<TDeclareEvaluationInfo> implements IEvaluationPointManageDao {

    @Override
    public void findDeclareEvaluationPointPageList(Page<TDeclareEvaluationPointVo> page, TDeclareEvaluationPointVo declareEvaluationPointVo) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.*,s.audit_status from t_declare_evaluation_point t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" LEFT JOIN t_declare_evaluation_info s on s.id = t.declare_id and s.del_sign = 0 ");
        sqlBuilder.append(" where t.del_sign = 0  ");
        sqlBuilder.append(" AND t.org_id=? ");
        arrayList.add(declareEvaluationPointVo.getOrgId());
        if (declareEvaluationPointVo.getYear() != null) {
            sqlBuilder.append(" AND t.year= ? ");
            arrayList.add(declareEvaluationPointVo.getYear());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getUserName())){
            sqlBuilder.append(" AND t.user_name LIKE concat('%',?,'%') ");
            arrayList.add(declareEvaluationPointVo.getUserName());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getErpNo())){
            sqlBuilder.append(" AND t.erp_no =? ");
            arrayList.add(declareEvaluationPointVo.getErpNo());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getDeptId())){
            sqlBuilder.append(" AND t.dept_id =? ");
            arrayList.add(declareEvaluationPointVo.getDeptId());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getAuditStatus())){
            sqlBuilder.append(" AND s.audit_status =? ");
            arrayList.add(declareEvaluationPointVo.getAuditStatus());
        }
        sqlBuilder.append(" order by t.create_time desc ");
        this.findPageBySql(TDeclareEvaluationPointVo.class, sqlBuilder.toString(), page, arrayList.toArray());
    }

    @Override
    public List<TDeclareEvaluationPointVo> findDeclareEvaluationPointList(TDeclareEvaluationPointVo declareEvaluationPointVo) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.*,s.audit_status from t_declare_evaluation_point t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" LEFT JOIN t_declare_evaluation_info s on s.id = t.declare_id and s.del_sign = 0 ");
        sqlBuilder.append(" where t.del_sign = 0  ");
        sqlBuilder.append(" AND t.org_id=? ");
        arrayList.add(declareEvaluationPointVo.getOrgId());
        if (declareEvaluationPointVo.getYear() != null) {
            sqlBuilder.append(" AND t.year= ? ");
            arrayList.add(declareEvaluationPointVo.getYear());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getUserName())){
            sqlBuilder.append(" AND t.user_name LIKE concat('%',?,'%') ");
            arrayList.add(declareEvaluationPointVo.getUserName());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getErpNo())){
            sqlBuilder.append(" AND t.erp_no =? ");
            arrayList.add(declareEvaluationPointVo.getErpNo());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getDeptId())){
            sqlBuilder.append(" AND t.dept_id =? ");
            arrayList.add(declareEvaluationPointVo.getDeptId());
        }
        if(!StringUtil.isNullOrEmpty(declareEvaluationPointVo.getAuditStatus())){
            sqlBuilder.append(" AND s.audit_status =? ");
            arrayList.add(declareEvaluationPointVo.getAuditStatus());
        }
        sqlBuilder.append(" order by t.create_time desc ");
        return this.findAllBySQL(TDeclareEvaluationPointVo.class, sqlBuilder.toString(),null,false,arrayList.toArray());
    }

    @Override
    public void saveInfo(TDeclareEvaluationPoint declareEvaluationPoint) {
        this.save(declareEvaluationPoint);
    }

    @Override
    public void updateEvaluationPoint(TDeclareEvaluationPoint declareEvaluationPoint) {
        String sql = "update t_declare_evaluation_point set ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        Map<String, Object> params = new HashMap<String, Object>();
        if(declareEvaluationPoint.getYearEffectPoint()!=null){
            sqlBuilder.append(" year_effect_point=#{year_effect_point,jdbcType=DOUBLE},");
            params.put("year_effect_point", declareEvaluationPoint.getYearEffectPoint());
        }
        if(declareEvaluationPoint.getContiStudyPoint()!=null){
            sqlBuilder.append(" conti_study_point=#{conti_study_point,jdbcType=DOUBLE},");
            params.put("conti_study_point", declareEvaluationPoint.getContiStudyPoint());
        }
        if(declareEvaluationPoint.getSdtTutorPrentPoint()!=null){
            sqlBuilder.append(" sdt_tutor_prent_point=#{sdt_tutor_prent_point,jdbcType=DOUBLE},");
            params.put("sdt_tutor_prent_point", declareEvaluationPoint.getSdtTutorPrentPoint());
        }
        if(declareEvaluationPoint.getHonorPoint()!=null){
            sqlBuilder.append(" honor_point=#{honor_point,jdbcType=DOUBLE},");
            params.put("honor_point", declareEvaluationPoint.getHonorPoint());
        }
        if(declareEvaluationPoint.getTecManPoint()!=null){
            sqlBuilder.append(" tec_man_point=#{tec_man_point,jdbcType=DOUBLE},");
            params.put("tec_man_point", declareEvaluationPoint.getTecManPoint());
        }
        if(declareEvaluationPoint.getTecInnovPoint()!=null){
            sqlBuilder.append(" tec_innov_point=#{tec_innov_point,jdbcType=DOUBLE},");
            params.put("tec_innov_point", declareEvaluationPoint.getTecInnovPoint());
        }
        if(declareEvaluationPoint.getPatentPoint()!=null){
            sqlBuilder.append(" patent_point=#{patent_point,jdbcType=DOUBLE},");
            params.put("patent_point", declareEvaluationPoint.getPatentPoint());
        }
        if(declareEvaluationPoint.getAcadePoint()!=null){
            sqlBuilder.append(" acade_point=#{acade_point,jdbcType=DOUBLE},");
            params.put("acade_point", declareEvaluationPoint.getAcadePoint());
        }
        if(declareEvaluationPoint.getStandPoint()!=null){
            sqlBuilder.append(" stand_point=#{stand_point,jdbcType=DOUBLE},");
            params.put("stand_point", declareEvaluationPoint.getStandPoint());
        }
        if(declareEvaluationPoint.getTecStandardPoint()!=null){
            sqlBuilder.append(" tec_standard_point=#{tec_standard_point,jdbcType=DOUBLE},");
            params.put("tec_standard_point", declareEvaluationPoint.getTecStandardPoint());
        }
//        if(declareEvaluationPoint.getExamPoint()!=null){
//            sqlBuilder .append(" exam_point=#{exam_point,jdbcType=DOUBLE},");
//            params.put("exam_point", declareEvaluationPoint.getExamPoint());
//        }
//        if(declareEvaluationPoint.getInterviewPoint()!=null){
//            sqlBuilder.append(" interview_point=#{interview_point,jdbcType=DOUBLE},");
//            params.put("interview_point", declareEvaluationPoint.getInterviewPoint());
//        }
        if(declareEvaluationPoint.getTotalPoint()!=null){
            sqlBuilder.append(" total_point=#{total_point,jdbcType=DOUBLE},");
            params.put("total_point", declareEvaluationPoint.getTotalPoint());
        }
        if(StringUtils.isNotEmpty(declareEvaluationPoint.getTrimReason())){
            sqlBuilder.append(" trim_reason=#{trim_reason,jdbcType=VARCHAR},");
            params.put("trim_reason", declareEvaluationPoint.getTrimReason());
        }
        sqlBuilder.append(" updater_name=#{updater_name,jdbcType=VARCHAR},")
                .append(" update_time=#{update_time,jdbcType=DATE} ");
        sqlBuilder.append(" where ID=#{id,jdbcType=VARCHAR}");
        params.put("updater_name", declareEvaluationPoint.getUpdaterName());
        params.put("update_time", declareEvaluationPoint.getUpdateTime());
        params.put("id", declareEvaluationPoint.getId());
        this.executeSql(sqlBuilder.toString(), params);

    }

    @Override
    public TDeclareEvaluationPointVo getDeclareEvaluationPoint(String year, String orgId, String erpNo) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.*,s.audit_status from t_declare_evaluation_point t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" LEFT JOIN t_declare_Evaluation_info s on s.id = t.declare_id and s.del_sign = 0 ");
        sqlBuilder.append(" where t.del_sign = 0 ");
        sqlBuilder.append(" AND t.year=? ");
        sqlBuilder.append(" AND t.org_id=? ");
        sqlBuilder.append(" AND t.erp_no=? ");
        if (!StringUtil.isNullOrEmpty(year)) {
            arrayList.add(year);
        }
        if(!StringUtil.isNullOrEmpty(orgId)){
            arrayList.add(orgId);
        }
        if(!StringUtil.isNullOrEmpty(erpNo)){
            arrayList.add(erpNo);
        }
        sqlBuilder.append(" order by t.create_time desc ");
        return this.findUnique(TDeclareEvaluationPointVo.class, sqlBuilder.toString(), arrayList.toArray());
    }

    @Override
    public void deleteDeclareEvaluationPoint(String declareId) {
        Map<String,Object> params = new HashMap<>();
        String sql = " delete from t_declare_evaluation_point where declare_id = #{declareId,jdbcType=VARCHAR} ";
        params.put("declareId",declareId);
        this.executeSql(sql,params);
    }

    @Override
    public TDeclareEvaluationPointVo selectTDeclareEvaluationPointById(String declareId){
        StringBuilder sqlBuilder = new StringBuilder(
                "select t.* FROM t_declare_evaluation_point t");
        sqlBuilder.append(" where t.declare_id=? ");
        ArrayList<Object> arrayList = new ArrayList<Object>();
        arrayList.add(declareId);
        return this.findUnique(TDeclareEvaluationPointVo.class, sqlBuilder.toString(), arrayList.toArray());
    }
}
